package com.ohuang.test_compose

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.widget.Toast
import java.lang.ref.WeakReference

class ContextUtil {
    companion object {

        var wContext:WeakReference<Context>?=null

        fun attach(context: Context){
            this.wContext=WeakReference(context)
        }


        fun toast(string:String){
            wContext?.let { it ->
                it.get()?.let {
                    Toast.makeText(it,string,Toast.LENGTH_SHORT).show()
                }

            }
        }

        fun getContext():Context?{
            wContext?.let {
                it.get()?.let { c->
                    return c
                }
            }
            return null
        }

        fun startActivity(intent:Intent){
            wContext?.let {
                it.get()?.let { c->
                    c.startActivity(intent)
                }}
        }
        fun startActivity(clazz: Class<out Activity>){
            startActivity(Intent(), clazz )
        }

        fun startActivity(intent: Intent,clazz: Class<out Activity>){
            wContext?.let {
                it.get()?.let { c->
                    intent.setClass(c,clazz)
                    startActivity(intent)
                }}
        }

        fun clear(){
            wContext=null
        }
    }

}